package com.lancoo.znbkxx.teacher.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.view.View
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.model.data.Paper
import com.lancoo.znbkxx.teacher.utils.HtmlUtils
import kotlinx.android.synthetic.main.item_paper_finished.view.tv_finished
import kotlinx.android.synthetic.main.item_paper_finished.view.tv_high_score
import kotlinx.android.synthetic.main.item_paper_finished.view.tv_papername
import kotlinx.android.synthetic.main.item_paper_finished.view.tv_total
import kotlinx.android.synthetic.main.item_paper_go_on.view.*
import kotlinx.android.synthetic.main.item_paper_unpublished.view.*
import java.text.DecimalFormat

/**
 * @author : wealive
 * @date   : 2020/7/2 20:35
 * @desc   :
 */
class SearchPaperListAdapter(data: MutableList<Paper>) :
    BaseMultiItemQuickAdapter<Paper, BaseViewHolder>(data) {
    init {
        addItemType(1, R.layout.item_paper_finished)
        addItemType(2, R.layout.item_paper_go_on)
        addItemType(3, R.layout.item_paper_unpublished)
    }

    @SuppressLint("SetTextI18n")
    override fun convert(helper: BaseViewHolder, item: Paper?) {
        with(helper) {
            //            if (item!!.t.EvalCount != 0) {
            addOnClickListener(R.id.tv_comment)
//            }
//            if (item.t.State != 2) {
            addOnClickListener(R.id.tv_detail)
//            }
            addOnClickListener(R.id.tv_publish)
            addOnClickListener(R.id.textView18)
            addOnClickListener(R.id.tv_total)
            addOnClickListener(R.id.tv_finished)
            addOnClickListener(R.id.tv_time)
        }
        with(helper.itemView) {
//            if (helper.layoutPosition == size + i) {
//                v_divider.visibility = View.GONE
//            } else {
//                v_divider.visibility = View.VISIBLE
//            }
            val paperName = item!!.PaperName.replace("（", "(").replace("）", ")")
            val str = SpannableStringBuilder(paperName)
            val span = ForegroundColorSpan(Color.parseColor("#999999"))
            str.setSpan(
                span,
                paperName.lastIndexOf("("),
                paperName.length,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            tv_papername.text = str
            if (helper.itemViewType == 1 || helper.itemViewType == 2) {
                if (item.EvalCount == 0) {
                    tv_comment.alpha = 0.3f
                } else {
                    tv_comment.alpha = 1.0f
                }
                tv_average_score.text = "平均分:${item.AverageScore}"
                tv_high_score.text = "最高分:${item.HighestScore}"
                tv_finished.text = item.SubmitCount.toString()
                tv_total.text = "/${item.StuCount}"
                if (helper.itemViewType == 2) {
                    if (item.State == 2) {
                        //                            view6.alpha = 0.3f
                        tv_detail.alpha = 0.3f
                    } else {
                        //                            view6.alpha = 1.0f
                        tv_detail.alpha = 1.0f
                    }
                    //                        pb.progressDrawable = mContext.getDrawable(R.drawable.shape_progressbar_mini_green)
                    tv_time.text = item.StartTime.replace("-", ".").substring(
                        0,
                        item.StartTime.length - 9
                    ) + "~" + item.EndTime.replace("-", ".").substring(
                        0,
                        item.EndTime.length - 9
                    )
                }
            } else if (helper.itemViewType == 3) {
                if (item.Layer == 3 || item.Layer == 4) {
//                    tv_average_score.visibility = View.GONE
                    tv_high_score.visibility = View.GONE
                    tv_effective.visibility = View.GONE
                    textView11.visibility = View.GONE
//                    textView12.visibility = View.GONE
                    textView14.visibility = View.GONE
                } else {
                    tv_average_score.visibility = View.VISIBLE
                    tv_high_score.visibility = View.VISIBLE
                    tv_effective.visibility = View.VISIBLE
                    textView11.visibility = View.VISIBLE
                    textView12.visibility = View.VISIBLE
                    textView14.visibility = View.VISIBLE
                }
//                tv_average_score.text = item.PaperQualityAnalyze.PaperKnowledgeRate
//                    .let { "${BigDecimal(it).setScale(2, RoundingMode.HALF_UP)}" + "%" }
//                tv_high_score.text = item.PaperQualityAnalyze.PaperMatchRate
//                    .let { "${BigDecimal(it).setScale(2, RoundingMode.HALF_UP)}" + "%" }
//                tv_effective.text = item.PaperQualityAnalyze.PaperEffective
//                    .let { "${BigDecimal(it).setScale(2, RoundingMode.HALF_UP)}" + "%" }
                tv_average_score.text = item.PaperQualityAnalyze.PaperKnowledgeRate
                tv_high_score.text = item.PaperQualityAnalyze.PaperMatchRate
                tv_effective.text = item.PaperQualityAnalyze.PaperEffective
                tv_difficult.text = HtmlUtils.setText(
                    mContext.resources.getString(
                        R.string.difficult,
                      item.PaperQualityAnalyze.PaperDifficulty
                    )
                )
                tv_discrimination.text = HtmlUtils.setText(
                    mContext.resources.getString(
                        R.string.discrimination,
                       item.PaperQualityAnalyze.PaperDistinguish
                    )
                )
                tv_exposure.text = HtmlUtils.setText(
                    mContext.resources.getString(
                        R.string.exposure,
                       item.PaperQualityAnalyze.PaperExposureRate
                    )
                )
            }
        }
    }


}